package com.mlming.springboot.config;

import com.mlming.springboot.interceptors.UserLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] addPathPatterns = {
                "/user/**","/shippings/**","/orders"
        };
        // 配置该拦截器不会拦截的请求路径(一般建立在 会拦截的范围 里面选几个特例)
        String[] excludePathPatterns = {
                "/carts","/carts/**","/error","/user/login","/user/register","/category/**","/product","/product/**"
        };
        // 相当于 <mvc:interceptor>标签里面的配置
        // addInterceptor(new UserInterceptor()) => 添加拦截器
        // addPathPatterns(拦截路径) => 添加该拦截器会拦截的路径的格式
        // excludePathPatterns(放行路径) => 添加该拦截器的不会拦截的路径格式
        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns);
    }
}
